﻿using UnityEngine;
using System.Collections;

public class DieFromBumping : MonoBehaviour {

	public float min_force = 12f;
	public GameObject die_fx;
	public GameObject create_on_destroy;

	void OnCollisionEnter2D (Collision2D c) {
	
		float f=0;
		
		if(c.rigidbody != null)
			f = Vector3.Dot (c.contacts[0].normal,c.relativeVelocity)*c.rigidbody.mass;
		else
			f = Vector3.Dot (c.contacts[0].normal,c.relativeVelocity);
	
		//print (Mathf.Abs(f));
	
		if(Mathf.Abs(f) >= min_force)
		{
			DieFromBumping d = c.gameObject.GetComponent<DieFromBumping>();
			if(d!=null)
			{
				if(d.die_fx != null)
				{
					GameObject explosion = Instantiate(d.die_fx,d.transform.position,d.transform.rotation)as GameObject;
					Destroy (explosion,explosion.particleSystem.startLifetime);
				}
				if(d.create_on_destroy != null)
				{
					GameObject item = Instantiate(d.create_on_destroy,d.transform.position,d.transform.rotation)as GameObject;
				}
				Destroy (d.gameObject);
			}
		
			//print (Mathf.Abs(f));
		
		
		
			if(die_fx != null)
			{
				GameObject explosion = Instantiate(die_fx,transform.position,transform.rotation)as GameObject;
				Destroy (explosion,explosion.particleSystem.startLifetime);
			}
			if(create_on_destroy != null)
			{
				GameObject item = Instantiate(create_on_destroy,transform.position,transform.rotation)as GameObject;
			}
			Destroy (gameObject);
		}
	
	}
}
